package com.example.demo.service.impl;

import com.example.demo.dao.BooksMapper;
import com.example.demo.pojo.*;
import com.example.demo.service.BooksService;
import com.example.demo.utils.Result;
import com.example.demo.utils.ResultFactory;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;

/**
 * 图书 Service
 */
@Service
public class BooksServiceImpl implements BooksService {

    @Autowired
    private BooksMapper booksMapper;

    private Logger logger = LoggerFactory.getLogger(BooksServiceImpl.class);

    @Override
    public Result saveBook(Stock stock,Books books) {

        Stock stock1 = this.booksMapper.selStockByStockNo(stock.getStock_no());
        if(stock1 == null){ //先插入
            this.booksMapper.insertStock(new Stock(stock.getStock_no(),new Date()));
            logger.info("BooksServiceImpl stock_id "+stock.getStock_id());
            books.setStock_id(stock.getStock_id());
        }else {
            books.setStock_id(stock1.getStock_id()); //库存编号
        }
        books.setIs_deletestate(1); //1-未出库,0-出库
        books.setLend_times(0); //借出次数 初始化为-0
        books.setBook_state(1);//图书状态 1-可借阅，2-暂停借阅，3-不可借阅
        int i = this.booksMapper.insertBook(books);
        if(i>0) {
            //图书借出状态 1-借出,2-出库，3-预约，4-归还,5-入库
            OutBound outBound = new OutBound(books.getBook_id(), 1, new Date(), 5);
            this.booksMapper.insertOutBound(outBound);
            return ResultFactory.successWithStatus(200, books.getName() + "入库成功");
        }
        //库存编号插入失败
        return ResultFactory.failureWithStatus(999, "图书入库失败");
    }

    @Override
    public Result updateBookById(Books books) {

        this.booksMapper.updateBooksInfoByBookId(books);
        return ResultFactory.successWithStatus(200,books.getName()+"图书更新成功");
    }

    @Override
    public List<ClassInfo> selAllClassInfo() {
        return this.booksMapper.selAllClassInfos();
    }



    @Override
    public Result saveClassInfo(ClassInfo classInfo) {
        int i = this.booksMapper.insertClassInfo(classInfo);
        if(i>0){
            return ResultFactory.successWithStatus(200,"添加分类成功");
        }
        return ResultFactory.failureWithStatus(999,"添加分类失败");
    }

    /**
     *
     * @param book_id  图书id  删除
     * @return
     */
    @Override
    public Result outBoundBooksById(int book_id) {
        Books books = this.booksMapper.selBooksByBookId(book_id);
        //book_state ;//图书状态 1-可借阅，2-暂停借阅，3-不可借阅
        books.setBook_state(3);
        books.setBook_id(book_id);
        //is_deletestate;//1-未出库,0-出库
        books.setIs_deletestate(0);
        this.booksMapper.updateBookStateByBookId(books);
        this.booksMapper.updateBookIsdeletestateByBookId(books);
        //添加出库表
        //图书借出状态 1-借出,2-出库，3-预约，4-归还,5-入库
        OutBound outBound1 = new OutBound(book_id,1,new Date(),2);
        this.booksMapper.insertOutBound(outBound1);
        return ResultFactory.successWithStatus(200,books.getName()+"出库成功");
    }


    @Override
    public Result selBookAndStockByBookId(int book_id) {
        Books books = this.booksMapper.selBookAndStockByBookId(book_id);
        if(books == null){
            return ResultFactory.failureWithStatus(999,"没有找到此本书");
        }
        return ResultFactory.successWithStatusAndData(200,"查找成功",books);
    }

    @Override
    public Result upateBookState(int book_id) {
        ///图书状态 1-可借阅，2-暂停借阅，3-不可借阅
        Books books = new Books();
        books.setBook_id(book_id);
        books.setBook_state(2);
        this.booksMapper.updateBookStateByBookId(books);
        return ResultFactory.successWithStatus(200,"修改图书为暂停借阅成功");
    }

    /**
     * 分页展示数据
     * @param currentPage 当前页数
     * @param pageSize
     * @return
     */
    public PageBean<Books> testPage(int currentPage,int pageSize){


        //设置分页信息，分别表示当前页数和每页显示总数
        int countNums = this.booksMapper.selAllBooks().size();

        PageHelper.startPage(currentPage,pageSize);
        List<Books> books = this.booksMapper.selAllBooks();
        PageInfo<Books> booksPageInfo = new PageInfo<>(books);
        List<Books> list = booksPageInfo.getList();
        PageBean<Books> pageData = new PageBean<>(currentPage,pageSize,countNums);
        pageData.setItem(list);


        return pageData;
    }


    @Override
    public PageBean<Books> queryBookInfoByConditions(Books books,int currentPage,int pageSize) {

        int i = this.booksMapper.selAllBookAndStockByConditions(books).size();
        if(i == this.booksMapper.selAllBooks().size()){
            return null;
        }

        PageHelper.startPage(currentPage,pageSize);
        List<Books> books1 = this.booksMapper.selAllBookAndStockByConditions(books);
        PageInfo<Books> booksPageInfo = new PageInfo<>(books1);
        List<Books> list = booksPageInfo.getList();
        PageBean<Books> pageData = new PageBean<>(currentPage,pageSize,i);
        pageData.setItem(list);
        return pageData;
    }

    @Override
    public Result updateBookClassByBookId(int book_id,int class_id) {

        this.booksMapper.updateBookClassInfoByBookId(book_id,class_id);
        return ResultFactory.successWithStatus(200,"图书转移成功");
    }
    //待修改
    @Override
    public List<Books> recommentBook() {
        List<Books> books = this.booksMapper.selLendTimesTop5Book();
        if(books.size() <=5){
            return  books;
        }
        return books.subList(0,4);
    }

    @Override
    public Result saveStock(Stock stock) {
        stock.setIn_time(new Date());
        int i = this.booksMapper.insertStock(stock);
        if(i>0){
            return ResultFactory.successWithData(200,"添加库存编号成功");
        }
        return ResultFactory.failureWithStatus(999,"添加库存编号失败");
    }

    @Override
    public Result queryBoosInfoByStockNo(String stock_no,int currentPage,int pageSize) {
        Stock stock = this.booksMapper.selStockByStockNo(stock_no);
        if(stock == null){
            return  ResultFactory.failureWithStatus(999,"不存在相关库存信息");
        }
        int i = this.booksMapper.selAllBookAndStockByStockId(stock.getStock_id()).size();

        PageHelper.startPage(currentPage,pageSize);
        List<Books> books = this.booksMapper.selAllBookAndStockByStockId(stock.getStock_id());
        PageInfo<Books> booksPageInfo = new PageInfo<>(books);
        List<Books> list = booksPageInfo.getList();
        PageBean<Books> pageData = new PageBean<>(currentPage,pageSize,i);
        pageData.setItem(list);
        return ResultFactory.successWithStatusAndData(200,"查询成功",pageData);
    }

    @Override
    public Result queryOutBoundList(int currentPage, int pageSize) {
        int i = this.booksMapper.selAllOutBoundCount();
        if(i==0){
            return  ResultFactory.failureWithStatus(999,"没有相关出入库信息");
        }
        PageHelper.startPage(currentPage,pageSize);
        List<OutBound> outBounds = this.booksMapper.selAllOutBound();
        PageInfo<OutBound> outBoundsPageInfo = new PageInfo<>(outBounds);
        List<OutBound> list = outBoundsPageInfo.getList();
        PageBean<OutBound> pageData = new PageBean<>(currentPage,pageSize,i);
        pageData.setItem(list);
        return ResultFactory.successWithStatusAndData(200,"查询成功",pageData);
    }
}
